<%@ page language="java" contentType="text/html;charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="com.yuxin.common.util.CollectionUtils" %>
<%@ page import="com.yuxin.common.util.DBUtil" %>
<%@ page import="com.yuxin.common.util.StringUtils" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %>
<%@ page import="com.yuxin.common.util.*" %>
<!DOCTYPE html>
<html>
<head>
<%@ include file="/meta.jsp"%>
<title>${site_title}</title>
<%
	Connection conn = DBUtil.getConnection();
	String id = request.getParameter("ID");
	if(StringUtils.hasText(id)){
		String sql = "select * from P_NOTE where id = ?";
		try{
			Map<String,Object> data = DBUtil.queryUniqueResult(conn, sql,id);
			request.setAttribute("data", data);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
%>
</head>
<body>
	<%@ include file="/header.jsp"%>
	<div class="container">
		<ul class="breadcrumb">
			<li><a href="${ctx}/admin/manager.jsp">首页</a> <span
				class="divider">/</span></li>
			<li><a href="${ctx}/admin/tuijian_l.jsp">商家推荐</a> <span
				class="divider">/</span></li>
			<li class="active">${param.ID == null?"新增":"编辑"}</li>
		</ul>
		<div class="span8">
			<form id="add_form" class="form-horizontal" method="post" action="${ctx}/admin/tuijian_action.jsp?action=add" enctype="multipart/form-data">
				<input type="hidden" name="TYPE" value="2">
				<input type="hidden" name="ID" value="${param.ID == null?data.ID:param.ID}">
        	<input type="hidden" id="imgPath" name="imgPath"  value="${param.IMG == null?data.IMG:param.IMG}"/>
				<div class="control-group">
					<label class="control-label" for="input01">标题</label>
					<div class="controls">
						<input type="text" class="input-xlarge" id="TITLE" name="TITLE" value="${param.TITLE == null?data.TITLE:param.TITLE}">
					</div>
				</div>
            <div class="control-group">
                <label class="control-label">图片</label>
                <div class="controls">
		            <span style="display:none;" class="help-block">请选择2M以内的.jpg|.gif|.png文件</span>
		            <div style="overflow:hidden;">
		            	<div id="img_preview" class="default-img left">
				            <img id="imgResShow" src="${ctx}/images/default.png">
		            	</div>
				   		<div id="del_btn" onclick="delImage(this);" class="left" style="cursor:pointer;z-index:1000;display:none;margin:-4px 0 0 -8px;">
				   			<img align="absmiddle" title="删除图片" style="width:20px;height:20px;" src="${ctx}/images/close.png">
				   		</div>
			   		</div>
                	<a href="javascript:void(0);" rel="tooltip" data-original-title="请选择2M以内的.jpg|.gif|.png文件" class="input-file btn">
		            	<input type="file" id="IMG" name="IMG" onchange="previewImage(this,'img_preview',100,80,imgPreview)"/>
		            	<i class="icon icon-picture"></i> 选择图片
		            </a>
                </div>
            </div>
	        <div class="row-fluid control-group">
	         	<label class="control-label">商品链结</label>
	            <div class="controls">
					<input type="text" class="input-xlarge" id="DESCRIPTION" name="DESCRIPTION" value="${param.DESCRIPTION == null?data.DESCRIPTION:param.DESCRIPTION}">
	            </div>
	        </div>
				<div class="control-group">
					<div class="controls">
						<label class="checkbox"> 
							<input type="checkbox" id="TARGET" name="TARGET" value="1" ${(param.TARGET == "1" || data.TARGET == "1")?"checked":""}> 发布到首页
						</label>
					</div>
				</div>
				<div class="form-actions">
					<button type="submit" class="btn btn-primary" onclick="doSave(this)">发布</button>
				</div>
			</form>
		</div>
	</div>
	<%@ include file="/footer.jsp"%>
</body>
<script src="${ctx}/js/kindeditor/kindeditor.js"></script>
<script src="${ctx}/js/kindeditor/lang/zh_CN.js"></script>
<script>

	var imgPath = $("#imgPath").val();
	
    $(document).ready(function(){
        if("${param.ID}"){
       	 $("#imgResShow").attr("src","${ctx}"+(imgPath ? imgPath+".s.jpg" : "/site/images/default.png"));
            if(imgPath){
            	$("#del_btn").show();
            }
          
       }
    });
	
    /*** 数据保存 ***/
    function doSave(ele){
        $(ele).button("loading");
        if(!$("#TITLE").validata("length",null,1,50)
                || !$("#TITLE").validata("length",null,0,100)
                || !$("#IMG").validata("blankOrImage")){
            $(ele).button('reset');
            return;
        }
        
        $("#add_form").submit();
    }
    
    /**图片验证**/
    function imgPreview(){
        if($("#IMG").validata("blankOrImage")){
	        $("#imgPath").val(imgPath);
            $("#del_btn").show();
            return true;
        }
    	return false;
    }


    /**图片删除**/
    function delImage(ele){
		$("#img_preview img").attr("src","${ctx}/images/default.png");
        $("#imgPath").val(imgPath);
		$("#IMG").val("");
		$(ele).hide();
    }
</script>